﻿#region Copyright (c) 2013-06, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

using System;
using Amarok.Agents;


namespace Amarok.Presentation
{
	/// <summary>
	/// This type provides extension methods for <see cref="IControllerFactory"/>.
	/// </summary>
	public static class ControllerFactoryExtensions
	{
		#region SpawnController(Type, IView, Object, AgentEnvironment)

		/// <summary>
		/// Spawns a new instance of the given controller type.
		/// </summary>
		/// 
		/// <param name="factory">
		/// The factory instance on which this method is called.</param>
		/// <param name="controllerType">
		/// The type of controller to spawn. This must be a sub-class of <see cref="Controller"/>.</param>
		/// <param name="view">
		/// A reference to the view with which the controller should be associated.</param>
		/// <param name="viewModel">
		/// The view model that is used as intermediate between controller and view.</param>
		/// <param name="environment">
		/// The agent environment to be supplied to the new controller instance.</param>
		/// 
		/// <returns>
		/// A reference to the newly spawned controller instance.</returns>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		/// <exception cref="ArgumentException">
		/// A value was passed to a method that did not accept it as a valid argument, because 
		/// the value representing a type must be a sub-class of <see cref="Controller"/>.</exception>
		/// <exception cref="ControllerSpawnException">
		/// An instance of the specified controller type cannot be instantiated, because the controller type does not 
		/// provide a controller-specific constructor that accepts agent environment, agent options, view and view model. 
		/// Provide a constructor with following signature: Controller(AgentEnvironment, AgentOptions, IView, Object).</exception>
		/// <exception cref="ControllerSpawnException">
		/// An instance of the specified controller type could not be spawned. Refer to the inner exception 
		/// for more information.</exception>
		public static IController SpawnController(
			this IControllerFactory factory,
			Type controllerType,
			IView view,
			Object viewModel,
			AgentEnvironment environment)
		{
			return factory.SpawnController(
				controllerType,
				view,
				viewModel,
				environment,
				AgentOptions.Empty);
		}

		#endregion


		#region SpawnController<T, M>(IView, AgentEnvironment, AgentOptions)

		/// <summary>
		/// Spawns a new instance of the given controller type.
		/// </summary>
		/// 
		/// <typeparam name="TController">
		/// The type of controller to spawn. This must be a sub-class of <see cref="Controller"/>.</typeparam>
		/// <typeparam name="TViewModel">
		/// The type of the view model used as intermediate between controller and view.</typeparam>
		/// 
		/// <param name="factory">
		/// The factory instance on which this method is called.</param>
		/// <param name="view">
		/// A reference to the view with which the controller should be associated.</param>
		/// <param name="environment">
		/// The agent environment to be supplied to the new controller instance.</param>
		/// <param name="options">
		/// The agent options to be supplied to the new controller instance.</param>
		/// 
		/// <returns>
		/// A reference to the newly spawned controller instance.</returns>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		/// <exception cref="ControllerSpawnException">
		/// An instance of the specified controller type cannot be instantiated, because the controller type does not 
		/// provide a controller-specific constructor that accepts agent environment, agent options and view. Provide 
		/// a constructor with following signature: Controller(AgentEnvironment, AgentOptions, IView).</exception>
		/// <exception cref="ControllerSpawnException">
		/// An instance of the specified controller type could not be spawned. Refer to the inner exception 
		/// for more information.</exception>
		public static IController SpawnController<TController, TViewModel>(
			this IControllerFactory factory,
			IView view,
			AgentEnvironment environment,
			AgentOptions options)
			where TController : Controller<TViewModel>
			where TViewModel : new()
		{
			return factory.SpawnController(
				typeof(TController),
				view,
				new TViewModel(),
				environment,
				options);
		}

		#endregion

		#region SpawnController<T, M>(IView, AgentEnvironment)

		/// <summary>
		/// Spawns a new instance of the given controller type.
		/// </summary>
		/// 
		/// <typeparam name="TController">
		/// The type of controller to spawn. This must be a sub-class of <see cref="Controller"/>.</typeparam>
		/// <typeparam name="TViewModel">
		/// The type of the view model used as intermediate between controller and view.</typeparam>
		/// 
		/// <param name="factory">
		/// The factory instance on which this method is called.</param>
		/// <param name="view">
		/// A reference to the view with which the controller should be associated.</param>
		/// <param name="environment">
		/// The agent environment to be supplied to the new controller instance.</param>
		/// 
		/// <returns>
		/// A reference to the newly spawned controller instance.</returns>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		/// <exception cref="ControllerSpawnException">
		/// An instance of the specified controller type cannot be instantiated, because the controller type does not 
		/// provide a controller-specific constructor that accepts agent environment, agent options and view. Provide 
		/// a constructor with following signature: Controller(AgentEnvironment, AgentOptions, IView).</exception>
		/// <exception cref="ControllerSpawnException">
		/// An instance of the specified controller type could not be spawned. Refer to the inner exception 
		/// for more information.</exception>
		public static IController SpawnController<TController, TViewModel>(
			this IControllerFactory factory,
			IView view,
			AgentEnvironment environment)
			where TController : Controller<TViewModel>
			where TViewModel : new()
		{
			return factory.SpawnController(
				typeof(TController),
				view,
				new TViewModel(),
				environment,
				AgentOptions.Empty);
		}

		#endregion

		#region SpawnController<T, M>(IView, Object, AgentEnvironment, AgentOptions)

		/// <summary>
		/// Spawns a new instance of the given controller type.
		/// </summary>
		/// 
		/// <typeparam name="TController">
		/// The type of controller to spawn. This must be a sub-class of <see cref="Controller"/>.</typeparam>
		/// <typeparam name="TViewModel">
		/// The type of the view model used as intermediate between controller and view.</typeparam>
		/// 
		/// <param name="factory">
		/// The factory instance on which this method is called.</param>
		/// <param name="view">
		/// A reference to the view with which the controller should be associated.</param>
		/// <param name="viewModel">
		/// The view model that is used as intermediate between controller and view.</param>
		/// <param name="environment">
		/// The agent environment to be supplied to the new controller instance.</param>
		/// <param name="options">
		/// The agent options to be supplied to the new controller instance.</param>
		/// 
		/// <returns>
		/// A reference to the newly spawned controller instance.</returns>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		/// <exception cref="ControllerSpawnException">
		/// An instance of the specified controller type cannot be instantiated, because the controller type does not 
		/// provide a controller-specific constructor that accepts agent environment, agent options and view. Provide 
		/// a constructor with following signature: Controller(AgentEnvironment, AgentOptions, IView).</exception>
		/// <exception cref="ControllerSpawnException">
		/// An instance of the specified controller type could not be spawned. Refer to the inner exception 
		/// for more information.</exception>
		public static IController SpawnController<TController, TViewModel>(
			this IControllerFactory factory,
			IView view,
			TViewModel viewModel,
			AgentEnvironment environment,
			AgentOptions options)
			where TController : Controller<TViewModel>
			where TViewModel : new()
		{
			return factory.SpawnController(
				typeof(TController),
				view,
				viewModel,
				environment,
				options);
		}

		#endregion

		#region SpawnController<T, M>(IView, Object, AgentEnvironment)

		/// <summary>
		/// Spawns a new instance of the given controller type.
		/// </summary>
		/// 
		/// <typeparam name="TController">
		/// The type of controller to spawn. This must be a sub-class of <see cref="Controller"/>.</typeparam>
		/// <typeparam name="TViewModel">
		/// The type of the view model used as intermediate between controller and view.</typeparam>
		/// 
		/// <param name="factory">
		/// The factory instance on which this method is called.</param>
		/// <param name="view">
		/// A reference to the view with which the controller should be associated.</param>
		/// <param name="viewModel">
		/// The view model that is used as intermediate between controller and view.</param>
		/// <param name="environment">
		/// The agent environment to be supplied to the new controller instance.</param>
		/// 
		/// <returns>
		/// A reference to the newly spawned controller instance.</returns>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		/// <exception cref="ControllerSpawnException">
		/// An instance of the specified controller type cannot be instantiated, because the controller type does not 
		/// provide a controller-specific constructor that accepts agent environment, agent options and view. Provide 
		/// a constructor with following signature: Controller(AgentEnvironment, AgentOptions, IView).</exception>
		/// <exception cref="ControllerSpawnException">
		/// An instance of the specified controller type could not be spawned. Refer to the inner exception 
		/// for more information.</exception>
		public static IController SpawnController<TController, TViewModel>(
			this IControllerFactory factory,
			IView view,
			TViewModel viewModel,
			AgentEnvironment environment)
			where TController : Controller<TViewModel>
			where TViewModel : new()
		{
			return factory.SpawnController(
				typeof(TController),
				view,
				viewModel,
				environment,
				AgentOptions.Empty);
		}

		#endregion


		#region SpawnController<T, M, E, O>(IView, AgentEnvrionment, AgentOptions)

		/// <summary>
		/// Spawns a new instance of the given controller type.
		/// </summary>
		/// 
		/// <typeparam name="TController">
		/// The type of controller to spawn. This must be a sub-class of <see cref="Controller"/>.</typeparam>
		/// <typeparam name="TViewModel">
		/// The type of the view model use as intermediate between controller and view.</typeparam>
		/// <typeparam name="TEnvironment">
		/// The type of agent environment to supply. This must be a sub-class of <see cref="AgentEnvironment"/>.</typeparam>
		/// <typeparam name="TOptions">
		/// The type of agent options to supply. This must be a sub-class of <see cref="AgentOptions"/>.</typeparam>
		/// 
		/// <param name="factory">
		/// The factory instance on which this method is called.</param>
		/// <param name="view">
		/// A reference to the view with which the controller should be associated.</param>
		/// <param name="environment">
		/// The agent environment to be supplied to the new controller instance.</param>
		/// <param name="options">
		/// The agent options to be supplied to the new controller instance.</param>
		/// 
		/// <returns>
		/// A reference to the newly spawned controller instance.</returns>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		/// <exception cref="ArgumentException">
		/// A value was passed to a method that did not accept it as a valid argument, because 
		/// the value representing a type must be a sub-class of <see cref="Controller"/>.</exception>
		/// <exception cref="ControllerSpawnException">
		/// An instance of the specified controller type cannot be instantiated, because the controller type does not 
		/// provide a controller-specific constructor that accepts agent environment, agent options and view. Provide 
		/// a constructor with following signature: Controller(AgentEnvironment, AgentOptions, IView).</exception>
		/// <exception cref="ControllerSpawnException">
		/// An instance of the specified controller type could not be spawned. Refer to the inner exception 
		/// for more information.</exception>
		public static IController SpawnController<TController, TViewModel, TEnvironment, TOptions>(
			this IControllerFactory factory,
			IView view,
			TEnvironment environment,
			TOptions options)
			where TController : Controller<TViewModel, TEnvironment, TOptions>
			where TViewModel : new()
			where TEnvironment : AgentEnvironment
			where TOptions : AgentOptions
		{
			return factory.SpawnController(
				typeof(TController),
				view,
				new TViewModel(),
				environment,
				options);
		}

		#endregion

		#region SpawnController<T, M, E, O>(IView, Object, AgentEnvrionment, AgentOptions)

		/// <summary>
		/// Spawns a new instance of the given controller type.
		/// </summary>
		/// 
		/// <typeparam name="TController">
		/// The type of controller to spawn. This must be a sub-class of <see cref="Controller"/>.</typeparam>
		/// <typeparam name="TViewModel">
		/// The type of the view model use as intermediate between controller and view.</typeparam>
		/// <typeparam name="TEnvironment">
		/// The type of agent environment to supply. This must be a sub-class of <see cref="AgentEnvironment"/>.</typeparam>
		/// <typeparam name="TOptions">
		/// The type of agent options to supply. This must be a sub-class of <see cref="AgentOptions"/>.</typeparam>
		/// 
		/// <param name="factory">
		/// The factory instance on which this method is called.</param>
		/// <param name="view">
		/// A reference to the view with which the controller should be associated.</param>
		/// <param name="viewModel">
		/// The view model that is used as intermediate between controller and view.</param>
		/// <param name="environment">
		/// The agent environment to be supplied to the new controller instance.</param>
		/// <param name="options">
		/// The agent options to be supplied to the new controller instance.</param>
		/// 
		/// <returns>
		/// A reference to the newly spawned controller instance.</returns>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		/// <exception cref="ArgumentException">
		/// A value was passed to a method that did not accept it as a valid argument, because 
		/// the value representing a type must be a sub-class of <see cref="Controller"/>.</exception>
		/// <exception cref="ControllerSpawnException">
		/// An instance of the specified controller type cannot be instantiated, because the controller type does not 
		/// provide a controller-specific constructor that accepts agent environment, agent options and view. Provide 
		/// a constructor with following signature: Controller(AgentEnvironment, AgentOptions, IView).</exception>
		/// <exception cref="ControllerSpawnException">
		/// An instance of the specified controller type could not be spawned. Refer to the inner exception 
		/// for more information.</exception>
		public static IController SpawnController<TController, TViewModel, TEnvironment, TOptions>(
			this IControllerFactory factory,
			IView view,
			TViewModel viewModel,
			TEnvironment environment,
			TOptions options)
			where TController : Controller<TViewModel, TEnvironment, TOptions>
			where TViewModel : new()
			where TEnvironment : AgentEnvironment
			where TOptions : AgentOptions
		{
			return factory.SpawnController(
				typeof(TController),
				view,
				viewModel,
				environment,
				options);
		}

		#endregion

	}
}
